package com.cskaoyan.service.wx;

import com.cskaoyan.bean.Brand;
import com.cskaoyan.bean.BrandExample;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.bean.vo.BrandDetailVO;
import com.cskaoyan.bean.vo.BrandListData;
import com.cskaoyan.mapper.BrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author heyyon
 */
@Service
public class WxBrandServiceImpl implements WxBrandService{
    @Autowired
    BrandMapper brandMapper;
    @Override
    public BrandListData<Brand> list(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        BrandExample brandExample = new BrandExample();
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        BrandListData<Brand> listData=new BrandListData<>();
        listData.setBrandList(brands);
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        listData.setTotal((int) total);
        return listData;
    }

    @Override
    public BrandDetailVO detail(Integer id) {
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria=brandExample.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        BrandDetailVO brandDetailVO = new BrandDetailVO();
        brandDetailVO.setBrand(brands.get(0));
        return brandDetailVO;
    }
}
